package com.ds.context;

import com.ds.esb.util.EsbFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

public class SpringMvcContextImpl extends JDSActionContext {
	public Map sessionMap = new HashMap();

	public Map contextMap = new HashMap();

	
	
	public SpringMvcContextImpl() {

	}

	@Override
	public Object Par(final String expression) {
		Object obj = null;
		try {
			obj = Par(expression, null);
		} catch (final Exception e) {

		}
		return obj;
	}

	@Override
	public Map getContext() {

		if (RequestContextHolder.getRequestAttributes() != null) {
			final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
			contextMap.putAll(request.getParameterMap());
		}

		return contextMap;
	}

	@Override
	public Object getParams(final String param) {
		String value = null;

		if (RequestContextHolder.getRequestAttributes() != null) {

			final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes();
			if (requestAttributes != null) {
				final HttpServletRequest request = requestAttributes.getRequest();
				if (request != null) {
					value = (String) request.getAttribute(param);

	                
					if ((value == null || value.equals(""))
                        && (request.getSession() != null)
						&& (request.getSession().getAttribute(param) != null)) {
						
						value = request.getSession().getAttribute(param).toString();
					}
				
					if (value == null||value.equals("")){
						final Object obj = getContext().get(param);
						if (obj != null ) {
							if (obj.getClass().isArray()) {
								final Object[] objs = (Object[]) getContext().get(
										param);
								if (objs.length > 0 && objs[0]!=null){
									value = objs[0].toString();
								}
									
							} else {
								value = obj.toString();
							}
						}

					}
					if (value == null){
							value = request.getParameter(param);
					}

					if (value == null){
						value = (String) sessionMap.get(param);
					}


					if (value == null){
						value = (String) contextMap.get(param);
					}



				}
			}

		}

		return value;
	}

	@Override
	public String getIpAddr() {
		String ip = null;
		if (RequestContextHolder.getRequestAttributes() != null) {
			final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
			ip = request.getHeader("X-Forwarded-For");

			final String gatewayip = request.getHeader("gateway-ip");
			if ((gatewayip != null) && (!gatewayip.equals(""))) {
				ip = gatewayip;
			}

			if ((ip == null) || (ip.length() == 0)
					|| ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if ((ip == null) || (ip.length() == 0)
					|| ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if ((ip == null) || (ip.length() == 0)
					|| ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if ((ip == null) || (ip.length() == 0)
					|| ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if ((ip == null) || (ip.length() == 0)
					|| ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getRemoteAddr();
			}
		}
		return ip;
	}

	@Override
	public Map getSession() {
		if (RequestContextHolder.getRequestAttributes() != null) {
			final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
			if (request != null) {
				final HttpSession session = request.getSession(true);
				sessionMap = ((Map) session.getAttribute("sessionMap"));
				if (sessionMap == null) {
					sessionMap = new HashMap();
					session.putValue("sessionMap", sessionMap);
				}
			}
		}

		return sessionMap;
	}

	@Override
	public String getSessionId() {
		String sessionid = null;
		if (RequestContextHolder.getRequestAttributes() != null) {
			final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();

			 sessionid = (String) getParams("JSESSIONID");
			
			 final String rSessionId= (String) getParams("RSESSIONID");
			 if ((rSessionId != null) && (!rSessionId.equals(""))){
				 sessionid=rSessionId;
			}
			if ((sessionid == null) || (sessionid.equals("")|| sessionid.equals("\"\""))) {
				sessionid	=request.getSession(true).getId();
				//sessionid = UUID.randomUUID().toString().toUpperCase();
				getSession().put("JSESSIONID", sessionid);
				request.getSession().setAttribute("JSESSIONID", sessionid);
			}
		}
		return sessionid;

	}

	@Override
	public Object Par(final String expression, final Class clazz) {
		return Par(expression, clazz, null);
	}

	@Override
	public Object Par(final String expression, final Class clazz, final Object source) {
		final Object o = EsbFactory.par(expression, contextMap,clazz);

		return o;
	}
	
	

	@Override
	public Object getHttpRequest() {
		HttpServletRequest request = null;
		if (RequestContextHolder.getRequestAttributes() != null) {
		 request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		}

		return request;
	}

	@Override
	public String getHost() {
		// TODO Auto-generated method stub
		return null;
	}

	
}
